﻿using App.Core.Entities;
using App.Core.Services;
using AppTemplate.UI.Dto;
using AppTemplate.UI.Framework;
using AutoMapper;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace AppTemplate.UI.ViewModels
{
    public partial class DataListViewModel : PagingViewModel<UserDto>
    {
        private readonly IUserService userService;
        private readonly IMapper mapper;

        public DataListViewModel(IUserService userService, IMapper mapper)
        {
            this.userService = userService;
            this.mapper = mapper;

            App.ShowStatusBar(true);
        }

        [RelayCommand]
        private void AddRow()
        {
            PageData.Add(new UserDto { IsModified = true });
        }

        protected override Task OnViewLoaded(CancellationToken cancellationToken)
        {
            if (IsLoaded)
            {
                return Task.CompletedTask;
            }
            else
            {
                IsLoaded = true;
                return base.OnViewLoaded(cancellationToken);
            }
        }

        protected override async Task LoadPageData()
        {
            App.ShowProgress(true, "正在加载用户");
            var pageResult = await this.userService.PageSearch(base.CurrentPage, PageSize);
            App.HideProgress();

            this.PageData.Clear();
            if (pageResult != null && pageResult.Data?.Any() == true)
            {
                base.TotalCount = pageResult.Total;
                base.PageCount = (int)Math.Ceiling(pageResult.Total / (double)PageSize);

                var allUsers = pageResult.Data;
                foreach (var u in allUsers)
                {
                    var UserDto = mapper.Map<UserDto>(u);
                    this.PageData.Add(UserDto);
                }
            }
        }


        [RelayCommand]
        private async Task Delete(UserDto? user)
        {
            if (user == null)
                return;
            else if (user.Id == 0)
            {
                PageData.Remove(user);
            }
            else if (App.Confirm("确定要删除该用户?", "正在删除用户", System.Windows.MessageBoxButton.OKCancel) == System.Windows.MessageBoxResult.OK)
            {
                await this.userService.Delete(user.Id);
                PageData.Remove(user);
            }

        }

        [RelayCommand]
        private async Task Save(UserDto dto)
        {


            if (dto.Id > 0)
            {
                User? existsOne = await userService.GetUserByAccount(dto.Account);
                if (existsOne != null && existsOne.Id != existsOne.Id)
                {
                    App.Alert("用户账号已存在");
                }
                else
                {
                    var entity = mapper.Map<User>(dto);

                    await userService.Update(entity);
                    App.Alert("保存成功");
                }


            }
            else
            {
                User? entity = await userService.GetUserByAccount(dto.Account);
                if (entity != null)
                {
                    App.Alert("用户账号已存在");
                }
                else
                {
                    entity = mapper.Map<User>(dto);

                    /*       var uid = AppState.UserId;
                           entity.CreateUserId = uid;*/
                    await userService.Create(entity);

                    dto.IsModified = false;
                    dto.Id = entity.Id;
                    App.Alert("保存成功");
                }


            }
        }
    }
}
